//
//  WeddingCarCartCell.m
//  BaseProject
//
//  Created by 王洋 on 2021/5/11.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "WeddingCarCartCell.h"
#import "WeddingCarSkuView.h"

@interface WeddingCarCartCell ()

@property (nonatomic, strong) WeddingCarSkuView *skuView;

@end

@implementation WeddingCarCartCell

+(CGFloat)calculateHeightWithModel:(WeddingCarCartModel *)model {
    
    CGFloat skuToTop = 16+16+5;
    CGFloat skuHeight = 16*model.userShoppingCarDetailList.count;
    CGFloat priceHeight = 30;
    CGFloat toBottom = 5;
    return skuToTop + skuHeight + priceHeight + toBottom;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.countTF.delegate = self;
    
    [self.skuBGView addSubview:self.skuView];
    [self.skuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    if (self.textFieldShouldEndEditingBlock) {
        self.textFieldShouldEndEditingBlock(textField);
    }
    return YES;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.cutBtn pp_clipCorners:UIRectCornerTopLeft|UIRectCornerBottomLeft radius:self.cutBtn.frame.size.height/2 border:1 color:UIColorHex(0xe4e4e4)];
        
        [self.addBtn pp_clipCorners:UIRectCornerTopRight|UIRectCornerBottomRight radius:self.addBtn.frame.size.height/2 border:1 color:UIColorHex(0xe4e4e4)];
        
        [self.mainImageView pp_clipCorners:UIRectCornerAllCorners radius:8];
    });
}

- (void)fillCellWithModel:(WeddingCarCartModel *)model {
    
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:model.specImg]];
    
    self.mainTitleLab.text = model.specName;
    
    
    self.countTF.text = [NSString stringWithFormat:@"%ld",model.goodsNum];
    
    [self.selBtn setImage:[UIImage imageNamed:model.isSel?@"common_sel":@"common_nor"] forState:UIControlStateNormal];
    
    [self.skuView fillViewWithArray:model.userShoppingCarDetailList andFollowNum:model.goodsNum];
    self.skuHeightConstrain.constant = 16*model.userShoppingCarDetailList.count;
    
    if (model.isCanBuy == 1) {
        self.priceLab.text = [NSString stringWithFormat:@"¥%@",[Utilities changeDoubleToString:model.totalPrice]];
        self.priceLab.textColor = UIColorHex(0xff4242);
        self.priceLab.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        self.invalidLab.hidden = YES;
        
        self.cutBtn.hidden = NO;
        self.addBtn.hidden = NO;
        self.countTF.hidden = NO;
        
        self.selBtn.enabled = YES;
        
    } else {
        
        self.priceLab.text = @"该规格暂时无法购买";
        self.priceLab.textColor = UIColorHex(0x333333);
        self.priceLab.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        self.invalidLab.hidden = NO;

        self.cutBtn.hidden = YES;
        self.addBtn.hidden = YES;
        self.countTF.hidden = YES;
        
        self.selBtn.enabled = NO;

    }

    
}

- (IBAction)addAction:(id)sender {
    if (self.WeddingCarCartCellBlock) {
        self.WeddingCarCartCellBlock(WeddingCarCartCellActionType_add);
    }
}

- (IBAction)cutAction:(id)sender {
    if (self.WeddingCarCartCellBlock) {
        self.WeddingCarCartCellBlock(WeddingCarCartCellActionType_cut);
    }
}

- (IBAction)selAction:(id)sender {
    if (self.WeddingCarCartCellBlock) {
        self.WeddingCarCartCellBlock(WeddingCarCartCellActionType_sel);
    }
}

- (WeddingCarSkuView *)skuView {
    if (!_skuView) {
        _skuView = [[WeddingCarSkuView alloc] init];
    }
    return _skuView;
}

@end
